﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SPMS.Utility;

namespace SPMS.Log.Appender
{
    /// <summary>
    /// 安全文本编写器
    /// </summary>
    /// <remarks>表示可以编写一个有序字符系列的安全编写器</remarks>
    /// <history>
    /// [zanezeng]               2009/11/25 12:00    创建
    /// </history>
    public sealed class SafeTextWriter : TextWriter
    {
        #region ---Constructor

        /// <summary>
        /// 初始化类型<see cref="SafeTextWriter"/>的新实例
        /// </summary>
        /// <param name="innerWriter">内部文本编写器</param>
        /// <remarks></remarks>
        /// <exception cref="ArgumentNullException"><paramref name="innerWriter"/>为Null</exception>
        /// <history>
        /// [zanezeng]               2009/11/25 12:03    创建
        /// </history>
        public SafeTextWriter( TextWriter innerWriter )
        {
            //断言参数
            ArgumentAssert.AssertArgumentNotNull( innerWriter, "innerWriter" );

            //保存参数
            this.m_InnerWriter = innerWriter;

            //阻止回收
            GC.SuppressFinalize( this.m_InnerWriter );
        }

        #endregion

        #region ---Public Method

        /// <summary>
        /// 获得编写器是否已经被关闭
        /// </summary>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/11/25 12:05    创建
        /// </history>
        public override void Close()
        {
            //设置已经关闭
            this.m_IsClosed = true;

            //关闭内部编写器
            this.m_InnerWriter.Close();
        }

        /// <summary>
        /// 清理当前编写器的所有缓冲区，使所有缓冲数据写入基础设备
        /// </summary>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/11/25 12:08    创建
        /// </history>
        public override void Flush()
        {
            //调用内部编写器
            this.m_InnerWriter.Flush();
        }

        /// <summary>
        /// 将<see cref="Boolean"/>值的文本表示形式写入文本流
        /// </summary>
        /// <param name="value">要写入的Boolean值</param>
        /// <exception cref="T:System.ObjectDisposedException">
        /// 	<see cref="T:System.IO.TextWriter"/>已经关闭
        /// </exception>
        /// <exception cref="T:System.IO.IOException">发生IO错误
        /// </exception>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/11/25 12:12    创建
        /// </history>
        public override void Write( bool value )
        {
            //调用内部编写器
            this.m_InnerWriter.Write( value );
        }

        /// <summary>
        /// 将<see cref="char"/>值的文本表示形式写入文本流
        /// </summary>
        /// <param name="value">要写入的char值</param>
        /// <exception cref="T:System.ObjectDisposedException">
        /// 	<see cref="T:System.IO.TextWriter"/>已经关闭
        /// </exception>
        /// <exception cref="T:System.IO.IOException">发生IO错误
        /// </exception>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/11/25 12:12    创建
        /// </history>
        public override void Write( char value )
        {
            //调用内部编写器
            this.m_InnerWriter.Write( value );
        }

        /// <summary>
        /// 将字符数组值的文本表示形式写入文本流
        /// </summary>
        /// <param name="buffer">要写入的char[]值</param>
        /// <exception cref="T:System.ObjectDisposedException">
        /// 	<see cref="T:System.IO.TextWriter"/>已经关闭
        /// </exception>
        /// <exception cref="T:System.IO.IOException">发生IO错误
        /// </exception>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/11/25 12:12    创建
        /// </history>
        public override void Write( char[] buffer )
        {
            //调用内部编写器
            this.m_InnerWriter.Write( buffer );
        }

        /// <summary>
        /// 将给定字符数组的子数组的文本表示形式写入文本流
        /// </summary>
        /// <param name="buffer">要写入的char[]值</param>
        /// <param name="index">在缓冲区中开始索引</param>
        /// <param name="count">要写入的字符数</param>
        /// <exception cref="T:System.ObjectDisposedException">
        /// 	<see cref="T:System.IO.TextWriter"/>已经关闭
        /// </exception>
        /// <exception cref="T:System.IO.IOException">发生IO错误
        /// </exception>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/11/25 12:12    创建
        /// </history>
        public override void Write( char[] buffer, int index, int count )
        {
            //调用内部编写器
            this.m_InnerWriter.Write( buffer, index, count );
        }

        /// <summary>
        /// 将<see cref="decimal"/>值的文本表示形式写入文本流
        /// </summary>
        /// <param name="value">要写入的decimal值</param>
        /// <exception cref="T:System.ObjectDisposedException">
        /// 	<see cref="T:System.IO.TextWriter"/>已经关闭
        /// </exception>
        /// <exception cref="T:System.IO.IOException">发生IO错误
        /// </exception>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/11/25 12:12    创建
        /// </history>
        public override void Write( decimal value )
        {
            //调用内部编写器
            this.m_InnerWriter.Write( value );
        }

        /// <summary>
        /// 将<see cref="double"/>值的文本表示形式写入文本流
        /// </summary>
        /// <param name="value">要写入的double值</param>
        /// <exception cref="T:System.ObjectDisposedException">
        /// 	<see cref="T:System.IO.TextWriter"/>已经关闭
        /// </exception>
        /// <exception cref="T:System.IO.IOException">发生IO错误
        /// </exception>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/11/25 12:12    创建
        /// </history>
        public override void Write( double value )
        {
            //调用内部编写器
            this.m_InnerWriter.Write( value );
        }

        /// <summary>
        /// 将<see cref="float"/>值的文本表示形式写入文本流
        /// </summary>
        /// <param name="value">要写入的float值</param>
        /// <exception cref="T:System.ObjectDisposedException">
        /// 	<see cref="T:System.IO.TextWriter"/>已经关闭
        /// </exception>
        /// <exception cref="T:System.IO.IOException">发生IO错误
        /// </exception>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/11/25 12:12    创建
        /// </history>
        public override void Write( float value )
        {
            //调用内部编写器
            this.m_InnerWriter.Write( value );
        }

        /// <summary>
        /// 将<see cref="int"/>值的文本表示形式写入文本流
        /// </summary>
        /// <param name="value">要写入的int值</param>
        /// <exception cref="T:System.ObjectDisposedException">
        /// 	<see cref="T:System.IO.TextWriter"/>已经关闭
        /// </exception>
        /// <exception cref="T:System.IO.IOException">发生IO错误
        /// </exception>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/11/25 12:12    创建
        /// </history>
        public override void Write( int value )
        {
            //调用内部编写器
            this.m_InnerWriter.Write( value );
        }

        /// <summary>
        /// 将<see cref="long"/>值的文本表示形式写入文本流
        /// </summary>
        /// <param name="value">要写入的long值</param>
        /// <exception cref="T:System.ObjectDisposedException">
        /// 	<see cref="T:System.IO.TextWriter"/>已经关闭
        /// </exception>
        /// <exception cref="T:System.IO.IOException">发生IO错误
        /// </exception>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/11/25 12:12    创建
        /// </history>
        public override void Write( long value )
        {
            //调用内部编写器
            this.m_InnerWriter.Write( value );
        }

        /// <summary>
        /// 将<see cref="object"/>值的文本表示形式写入文本流
        /// </summary>
        /// <param name="value">要写入的object值</param>
        /// <exception cref="T:System.ObjectDisposedException">
        /// 	<see cref="T:System.IO.TextWriter"/>已经关闭
        /// </exception>
        /// <exception cref="T:System.IO.IOException">发生IO错误
        /// </exception>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/11/25 12:12    创建
        /// </history>
        public override void Write( object value )
        {
            //调用内部编写器
            this.m_InnerWriter.Write( value );
        }

        /// <summary>
        /// 将行结束符写入文本流
        /// </summary>
        /// <exception cref="T:System.ObjectDisposedException">
        /// 	<see cref="T:System.IO.TextWriter"/>已经关闭
        /// </exception>
        /// <exception cref="T:System.IO.IOException">发生IO错误
        /// </exception>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/11/25 12:12    创建
        /// </history>
        public override void WriteLine()
        {
            //调用内部编写器
            this.m_InnerWriter.WriteLine();
        }

        #endregion

        #region ---Property

        /// <summary>
        /// 获得用于写输出的<see cref="Encoding"/>
        /// </summary>
        /// <value></value>
        /// <returns>
        /// 用于写输出的<see cref="Encoding"/>
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/11/25 12:03    创建
        /// </history>
        public override Encoding Encoding
        {
            get
            {
                return this.m_InnerWriter.Encoding;
            }
        }

        /// <summary>
        /// 获取控制格式设置的对象
        /// </summary>
        /// <value></value>
        /// <returns>
        /// 特定区域性的<see cref="T:System.IFormatProvider"/>对象，或者如果未指定任何其他区域性，则为当前区域性的格式设置
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/11/25 12:09    创建
        /// </history>
        public override IFormatProvider FormatProvider
        {
            get
            {
                return this.m_InnerWriter.FormatProvider;
            }
        }

        /// <summary>
        /// 获取或设置由当前使用的行结束符字符串
        /// </summary>
        /// <value>当前使用的行结束符字符串</value>
        /// <returns>
        /// 当前<see cref="TextWriter"/>的行结束符字符串。
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/11/25 12:11    创建
        /// </history>
        public override string NewLine
        {
            get
            {
                return this.m_InnerWriter.NewLine;
            }
            set
            {
                this.m_InnerWriter.NewLine = value;
            }
        }

        /// <summary>
        /// 获得一个值，该值用于判断当前编写器是否已经关闭
        /// </summary>
        /// <value>
        /// 	<c>true</c>：编写器已经关闭<br/>
        /// 	<c>false</c>：编写器没有关闭
        /// </value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/11/25 12:24    创建
        /// </history>
        public bool IsClosed
        {
            get
            {
                return this.m_IsClosed;
            }
        }

        #endregion

        #region ---Field

        /// <summary>
        /// 内部编写器
        /// </summary>
        private TextWriter m_InnerWriter;

        /// <summary>
        /// 记录是否已经被关闭
        /// </summary>
        private bool m_IsClosed;

        #endregion
    }
}
